package pojo;

import com.alibaba.fastjson.JSONObject;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import util.JSONFormatUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @author Administrator
 */
public class RequestPoJo implements Serializable {

    private JSONObject json;
    private String otherParam;

    public JSONObject getJson() {
        json = Optional.ofNullable(json).orElse(new JSONObject());
        return json;
    }

    public void setJson(JSONObject json) {
        if( json == null){
            return ;
        }
        // 过滤空值
        JSONFormatUtil.removeEmpty(json);

        JSONObject tempJson = JSONObject.parseObject(json.toJSONString());
        if (tempJson.containsKey("sessionContext")) {
            tempJson.remove("sessionContext");
        }
        if (tempJson.containsKey("page")) {
            tempJson.remove("page");
        }
        this.otherParam = tempJson.toJSONString();
        this.json = json;
    }

    public String getOtherParam() {
        otherParam = Optional.ofNullable(otherParam).orElse("");
        return otherParam;
    }

    public void setOtherParam(String otherParam) {
        this.otherParam = otherParam;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        RequestPoJo that = (RequestPoJo) o;
        return otherParam.equals(that.otherParam);
    }

    @Override
    public int hashCode() {
        return Objects.hash(otherParam);
    }

    public RequestPoJo(JSONObject json) {
        this.setJson(json);
    }

    public RequestPoJo() {
    }
}
